package so.shanku.essential.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.fragment.BaseFragment;
import so.shanku.essential.fragment.ManBrandHotFragment;
import so.shanku.essential.fragment.ManBrandListFragment;
import so.shanku.essential.utils.Constant;

import java.util.HashMap;

/**
 * Created by Administrator on 2015/9/23.
 */
public class ManBrandActivity extends BaseUIActivity {

	@ViewInject(id = R.id.rg_man_brand)
	private RadioGroup rg_man_brand;
	private int brandsId;
	private Bundle bundle;
	/**
	 * 保存点击过得fragment
	 */
	private HashMap<Integer, BaseFragment> fragments = new HashMap();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_man_brand);
		setCenter_title(getIntent().getBundleExtra(Constant.BUNDLE_EXTRA).getString(Constant.NAME));
		brandsId = getIntent().getBundleExtra(Constant.BUNDLE_EXTRA).getInt(Constant.ID);
		bundle = getIntent().getBundleExtra(Constant.BUNDLE_EXTRA);
		selectHotFragment();
		initCheck();
	}

	public void initCheck() {
		rg_man_brand.setOnCheckedChangeListener(
				new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						for (int i = 0; i < group.getChildCount(); i++) {
							RadioButton radioButton = (RadioButton) group.getChildAt(i);
							if (radioButton.getId() == checkedId) {
								switch (i) {
									case 0:
										selectHotFragment();

										break;
									case 1:
										selecListFragment();
										break;

								}
							}

						}

					}
				});

	}

	private void selecListFragment() {
		if (fragments.get(1) != null) {
			replaceView(fragments.get(1));
		} else {
			ManBrandListFragment manBrandListFragment = new ManBrandListFragment();
			manBrandListFragment.setArguments(bundle);
			replaceView(manBrandListFragment);
			fragments.put(1, manBrandListFragment);
		}
	}

	private void selectHotFragment() {
		if (fragments.get(0) != null) {
			replaceView(fragments.get(0));
		} else {
			//HomePageFragment homePageFragment=new HomePageFragment();
			ManBrandHotFragment manBrandHotFragment = new ManBrandHotFragment();
			manBrandHotFragment.setArguments(bundle);
			replaceView(manBrandHotFragment);
			fragments.put(0, manBrandHotFragment);
		}
	}

	private Fragment currentFragment;

	private void replaceView(BaseFragment fragment) {
		if (currentFragment != fragment) {
			FragmentTransaction transaction = this.getSupportFragmentManager()
					.beginTransaction();
//			FragmentTransaction transaction = mFragmentMan.beginTransaction().setCustomAnimations(
//					android.R.anim.fade_in, R.anim.slide_out);
			if (currentFragment != null) {
				if (!fragment.isAdded()) {    // 先判断是否被add过
					transaction.hide(currentFragment).add(R.id.linearlayout_manbrand, fragment).commit(); // 隐藏当前的fragment，add下一个到Activity中
				} else {
					transaction.hide(currentFragment).show(fragment).commit(); // 隐藏当前的fragment，显示下一个
				}
			} else {
				transaction.add(R.id.linearlayout_manbrand, fragment).commit(); // add下一个到Activity中
			}
			currentFragment = fragment;
		}
	}
}